<?php
require(dirname(dirname(__FILE__))."/Config/config.php");
require(dirname(dirname(__FILE__))."/jssdk.php");
class chatController extends appController{
	public $wxid = 0;//数据库Id
	
	public $m;//自己的model
	public $info=array();//当前用户的微信信息
	public  function __construct(){
		parent::__construct();
	
		//基础信息构建
		
		$this->m = Model('chat');
		//微信分享参数构建
		$jsdk = $this->m ->get_jsdk();
		
		$this->assign("signPackage", $jsdk);
	
		//当前用户的微信id
		$this->wxid = $_SESSION['wxid']?$_SESSION['wxid']:0;
		
		if($this->wxid>0)
		{
			//当前用户的微信信息
			$this->info = $this->m->get_record($this->wxid);
			$this->assign('info',$this->info);
		}
	}
	
	public function set()
	{
		$_SESSION['wxid']=$_GET['wxid'];
	}
	//清除数据
	public function clear()
	{
	
		unset($_SESSION['wxid']);
		unset($_SESSION['pid']);
		echo "已清除数据";
		exit;
	}
	//需要授权才可进入游戏
	public function canIn()
	{
		if(!$this->info)
		{
			header("Location:http://".$_SERVER['HTTP_HOST']."/ws/index.php?g=home&m=chat&a=index");
			exit;
		}
	}
	
	//微信访问地址
	public function index(){
		
		if(!isset($_SESSION['wxid']))
		{
			//是否已授权
			$this->check_member();
		}else{
			$config[c] = array('site_name' => "G4弹幕-".Model('app')->get_cfg('site_name'));
			
			$this->assign_global($config);
			//记录登陆信息
			$this->m->save_visitor($this->info['openid']);
			$result = $this->m->get_special_chat($this->wxid);
			$this->assign('rule1', json_encode($result['rule1']));//时间
			$this->assign('rule2', json_encode($result['rule2']));//条数
			$this->assign('rule3', json_encode($result['rule3']));//每日
			$this->assign('rule4', json_encode($result['rule4']));//单日
			$timestamp =intval($this->m->get_timestamp());
			$refresh = intval($this->m->get_refreshtime());
			$commentDayMax = intval($this->m->get_cfg('commentDayMax'));
			$commentMinuteMax = intval($this->m->get_cfg('commentMinuteMax'));
			$chatMaxNum = intval($this->m->get_cfg('chatMaxNum'));
			$staggerTime = intval($this->m->get_cfg('staggerTime'));
			$cfg = $this->m->get_cfg();
			//获取个人当天评论的个数
			$commentDayNum = $this->m->get_comment_num($this->wxid);
			//查询轮播图片
			$carousel = $this->m->get_indexad(18);
			$background = $this->m->get_indexad(19);
			$special_ad = $this->m->get_special_ad();
			$this->assign('specialAd',json_encode($special_ad));
			$this->assign('carouselAd',json_encode($carousel));
			$time = time();
			$this->assign('timeout',$refresh-($time-$timestamp)%$refresh);
			$this->assign('refreshtime',$refresh);
			$this->assign('commentDayMax',$commentDayMax);
			$this->assign('commentMinuteMax',$commentMinuteMax);
			$this->assign('commentDayNum',$commentDayNum);
			$this->assign('placeholder',$cfg['placeholder']);
			$this->assign('staggerTime',$staggerTime);
			$this->assign('chatMaxNum',$chatMaxNum);
			$this->assign('carousel',$carousel);
			$this->assign('background',$background[0]);
			$num = rand(0,10);
			$this->assign("timestamp", $timestamp+$num);
			$this->assign("now", $time);
			$this->display('index2.html');
		}
		
	}
	/**
	 * 保存评论
	 * @return text
	 */
	public function saveComment(){
		$this->canIn();
		$data['comment'] = $_POST['text'];
		$data['wxid'] = $this->wxid;
		$data['status'] =0;
		
		$res =$this->m->create_record($data);
		print_r(json_encode($res));
// 		if($res==4004){
// 			echo "silence";
// 		}elseif ($res ==3003){
// 			echo "false";
// 		}else{
// 			echo "ok";
// 		}
	}
	
	function parseurl($url="")
	{
		$url = rawurlencode(mb_convert_encoding($url, 'gb2312', 'utf-8'));
	
		return $url;
	}
	
	public function addClickAd(){
		$id= isset($_POST['id'])?$_POST['id']:"";
		$res = array("error"=>1);
		if($id){
			if($this->m->add_click_num($id)){
				$res['error']=0;
			}
		}
		print_r(json_encode($res));
	}
	
	public function getBox(){
		$this->canIn();
		$group_id = $_POST['group_id'];
		$res = $this->m->get_box($group_id,$this->info['wxid']);
		print_r(json_encode($res));
	}
	
	public function saveInfo(){
		$this->canIn();
		$info['phone'] =isset($_POST['phone'])?$_POST['phone']:"";
		if(preg_match("/1[3458]{1}\d{9}$/",$info['phone'])){
		
		}else{
			$res['error'] = 0;
			$res['content'] = "请填写正确的手机号码";
			die(json_encode($res));
		}
		$info['group_id'] = $_POST['group_id'];
		$info['status_id'] = isset($_POST['status_id'])?$_POST['status_id']:"";
		$info['name'] = isset($_POST['name'])?$_POST['name']:"";
		$info['addtime'] = $_SERVER['REQUEST_TIME'];
		$info['wxid'] = $this->info['wxid'];
		$res = Model('chat')->save_info($info);
		print_r(json_encode($res));
	}
	
	
}